<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>
  
<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '450px'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            type: Object,
            required: true
        },
    },
    data() {
        return {
            chart: null
        }
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                this.setOptions(val)
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            this.setOptions(this.chartData)
        },
        setOptions({ Data, dateData } = {}) {
            this.chart.setOption({
                title: {
                    text: '周活跃用户'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {},
                // toolbox: {
                //   show: true,
                //   feature: {
                //     dataZoom: {
                //       yAxisIndex: 'none'
                //     },
                //     dataView: { readOnly: false },
                //     magicType: { type: ['line', 'bar'] },
                //     restore: {},
                //     saveAsImage: {}
                //   }
                // },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: dateData
                },
                yAxis: {
                    type: 'value',
                    axisLabel: {
                        formatter: '{value} 人'
                    }
                },
                series: [
                    // {
                    //   name: 'Highest',
                    //   type: 'line',
                    //   data: [10, 11, 13, 11, 12, 12, 9],
                    //   markPoint: {
                    //     data: [
                    //       { type: 'max', name: 'Max' },
                    //       { type: 'min', name: 'Min' }
                    //     ]
                    //   },
                    //   markLine: {
                    //     data: [{ type: 'average', name: 'Avg' }]
                    //   }
                    // },
                    {
                        name: '每日人数',
                        type: 'line',
                        data: Data,
                        // markPoint: {
                        //   data: [{ name: '周最低', value: -2, xAxis: 1, yAxis: -1.5 }]
                        // },
                        markLine: {
                            data: [
                                { type: 'average', name: 'Avg' },
                                [
                                    {
                                        symbol: 'none',
                                        x: '90%',
                                        yAxis: 'max'
                                    },
                                    {
                                        symbol: 'circle',
                                        label: {
                                            position: 'start',
                                            formatter: 'Max'
                                        },
                                        type: 'max',
                                        name: '最高点'
                                    }
                                ],
                                [
                                    {
                                        symbol: 'none',
                                        x: '90%',
                                        yAxis: 'min'
                                    },
                                    {
                                        symbol: 'circle',
                                        label: {
                                            position: 'start',
                                            formatter: 'Min'
                                        },
                                        type: 'min',
                                        name: '最高点'
                                    }
                                ]
                            ]
                        }
                    }
                ]
            })
        }
    }
}
</script>
  